//
//  HCScrollContentView.swift
//  HappyComics
//
//  Created by 金玉龙 on 15/11/27.
//  Copyright © 2015年 jinyulong. All rights reserved.
//

import UIKit

class HCScrollContentView: UIView ,HCScrollTitleViewDelegate{
    let titleView : HCScrollTitleView = HCScrollTitleView()
    var titleArray : NSArray?
    var selectedIndex : Int?
    var viewsArray : NSArray?


    func initScrollContentViewWithArray(titleArray : NSArray? , viewArray viewsArray : NSArray?, selectedIndex index :Int) ->Void{
        self.titleArray = NSArray.init(array: titleArray!)
        self.viewsArray = NSArray.init(array: viewsArray!)
        self.selectedIndex = index
        self.loadTitleView()
//        self.loadScrollView()
    }
    
    private func loadTitleView() ->Void{
        self.addSubview(titleView)
        unowned let weakSelf = self
        titleView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(weakSelf)
            make.height.equalTo(44)
        }
        titleView.initWithTitleArray(self.titleArray, selectedIndex: self.selectedIndex)
        titleView.btnBorderColor = UIColor.blackColor()
        titleView.btnBorderWidth = 0.3
        titleView.updateButtonPropertys()
        titleView.delegate = self
    }
    
    private func loadScrollView() ->Void{
        let scroll = UIScrollView.init()
        self.addSubview(scroll)
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scroll]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["scroll" : scroll]))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scroll]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["scroll" : scroll]))
        
        var pageCount = 0
        var lastView : UIView?
        
        for view in self.viewsArray!{
            pageCount++
            let currentView = view as! UIView
            currentView.backgroundColor = UIColor.blueColor()
            scroll.addSubview(currentView)
            
            //等高
            scroll.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[currentView]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil , views: ["currentView" : currentView]))
            //等宽
            scroll.addConstraint(NSLayoutConstraint.init(item: currentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: scroll, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
            
            if lastView == nil{
                scroll.addConstraint(NSLayoutConstraint.init(item: currentView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: scroll, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
            }else{
                scroll.addConstraint(NSLayoutConstraint.init(item: currentView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: lastView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
            }
            lastView = currentView
        }
        
        scroll.addConstraint(NSLayoutConstraint.init(item: lastView!, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: scroll, attribute: NSLayoutAttribute.Right, multiplier: 2, constant: 0))
        
        

        scroll.backgroundColor = UIColor.blueColor()
        scroll.contentSize = CGSizeMake(self.frame.width * CGFloat(pageCount), self.frame.height)
        scroll.pagingEnabled = true
        self.layoutIfNeeded()
        
        
    }
    
    func titleView(scrollTitleView: HCScrollTitleView, didSelectedIndex index: Int) {
        print(index)
        print(scrollTitleView.titlesArray?.objectAtIndex(index))
    }
}
